//
//  JKSkinningTool.m
//  JKOCSkinning
//
//  Created by 王冲 on 2019/1/28.
//  Copyright © 2019年 JK科技有限公司. All rights reserved.
//

#import "JKSkinningTool.h"

#define kSkinNameKey @"skinName"

@implementation JKSkinningTool

+(void)saveBgImagePath:(SkinningType)imageName{
    
    NSString *name = [self chooseImageName:imageName];
    
    NSString *path = [NSString stringWithFormat:@"JKSkinningTool.bundle/skin/%@",name];
    [[NSUserDefaults standardUserDefaults]setValue:path forKey:kSkinNameKey];
    [[NSUserDefaults standardUserDefaults]synchronize];
}

#pragma mark 获取当前主题下图片的名字
+(NSString *)getBgImageName
{
    
    return [[NSUserDefaults standardUserDefaults]valueForKey:kSkinNameKey];;
}

+(UIImage *)getBgImage:(JKSkinImageType)imageType
{
    
    NSString *skinName = [self getBgImageName];
    NSString *skinImageType = [self chooseSkinImageType:imageType];
    return [UIImage imageNamed:[NSString stringWithFormat:@"%@%@",skinName,skinImageType]];
}

// 当前主题下的文字颜色
+ (UIColor *)getSkinLabekColorWithType:(JKSkinColorType)type{
    
    // 1.拿到路径的名字
    NSString *skinName = [self getBgImageName];
    NSString *skinColorStr = [self chooseLabelColor:type];
    
    // 2.拼接plst文件的路径
    NSString *path = [[NSBundle mainBundle]pathForResource:@"JKSkinColorConfig.plist" ofType:nil inDirectory:skinName];
    // 3.取出路径下的字典
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    // 3.1、取出字典类里面的数组
    NSArray *array = dict[skinColorStr];
    if (array.count < 3) {
        
        return nil;
    }
    
    CGFloat red = [((NSString *)array[0]) floatValue];
    CGFloat green = [((NSString *)array[1]) floatValue];
    CGFloat blue = [((NSString *)array[2]) floatValue];
    
    UIColor *color = [UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1];
    
    return color;
}

+(NSString *)chooseImageName:(SkinningType)type{
    
    NSString *typeName;
    switch (type) {
        case ChunJie:
            typeName = @"chunjie/";
            break;
        case GuoQing:
            typeName = @"guoqing/";
            break;
        case ZhongQiu:
            typeName = @"zhongqiu/";
            break;
        default:
            break;
    }
    return typeName;
}

+(NSString *)chooseSkinImageType:(JKSkinImageType)type{
    
    NSString *typeName;
    switch (type) {
        case SkinBack:
            typeName = @"back.png";
            break;
        case SkinIcon:
            typeName = @"button_back.png";
            break;
        default:
            break;
    }
    return typeName;
}

+(NSString *)chooseLabelColor:(JKSkinColorType)type{
    
    NSString *typeName;
    switch (type) {
        case SkinLabelColor:
            typeName = @"SkinLabelColor";
            break;
        default:
            break;
    }
    return typeName;
}

@end
